package com.itheima.tlias.controller;

import com.itheima.tlias.dto.SelectStudentByIdDTO;
import com.itheima.tlias.entity.Student;
import com.itheima.tlias.po.InsertStudentParam;
import com.itheima.tlias.po.SelectStudentPageParam;
import com.itheima.tlias.po.UpdateStudentParam;
import com.itheima.tlias.service.StudentService;
import com.itheima.tlias.util.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
@RequestMapping("students")
public class StudentController {

    @Resource
    StudentService studentService;

    @GetMapping
    public ResultUtil selectStudentByPage(SelectStudentPageParam param){
        if (Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())) {
            return ResultUtil.fail("分页参数不存在");
        }
        return studentService.selectStudentByPage(param);
    }

    @PostMapping
    public ResultUtil insertStudent(@Validated @RequestBody InsertStudentParam param){
        int flag = studentService.insertStudent(param);
        return flag == 0 ? ResultUtil.fail("添加失败") : ResultUtil.success("添加学员信息成功");
    }

    @DeleteMapping("/{ids}")
    public ResultUtil batchDeleteStudent(@PathVariable List<Integer> ids){
        if (ids.isEmpty()) {
            return ResultUtil.fail("请先选择要删除的信息");
        }
        int flag = studentService.batchDeleteStudent(ids);
        return ResultUtil.success(flag);
    }

    @GetMapping("{id}")
    public ResultUtil selectStudentById(@PathVariable Integer id){
        Student dto = studentService.selectStudentById(id);
        return ResultUtil.success(dto);
    }

    @PutMapping
    public ResultUtil updateStudent(@RequestBody Student param){
        int flag = studentService.updateStudent(param);
        return flag == 0 ? ResultUtil.fail("修改失败") : ResultUtil.success("修改成功");
    }

    @PutMapping("/violation/{id}/{score}")
    public ResultUtil updateViolation(@PathVariable Integer id,@PathVariable Integer score){
        int flag = studentService.updateViolation(id,score);
        return ResultUtil.success(flag);
    }
}
